package com.wqiuqiu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.R;
import com.wqiuqiu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wqiuqiu.gulimall.product.entity.AttrEntity;
import com.wqiuqiu.gulimall.product.service.AttrAttrgroupRelationService;
import com.wqiuqiu.gulimall.product.service.AttrService;
import com.wqiuqiu.gulimall.product.service.CategoryService;
import com.wqiuqiu.gulimall.product.vo.AttrAttrGroupVo;
import com.wqiuqiu.gulimall.product.vo.RelGroupAndAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wqiuqiu.gulimall.product.entity.AttrGroupEntity;
import com.wqiuqiu.gulimall.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author wqiuqiu
 * @email 18773489795@163.com
 * @date 2023-12-11 12:32:41
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelAttrlist(@RequestParam Map<String, Object> params,
                              @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.queryNoRelAttrPage(params, attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getRelGroupAndAttrlist(@PathVariable("catelogId") Long catelogId) {
        List<RelGroupAndAttrVo> data = attrGroupService.queryRelGroupAndAttr(catelogId);
        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.getCatelogIdPaths(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 属性组关联的全部属性信息
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrInfo(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> data = attrGroupService.findAttrList(attrgroupId);

        return R.ok().put("data", data);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 新增属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveAttrRel(@RequestBody List<AttrAttrGroupVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRel(@RequestBody AttrAttrGroupVo[] vos) {
        attrAttrgroupRelationService.removeRel(vos);
        return R.ok();
    }

}
